package com.chuangmei.com.arindoorlocationsystem.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.chuangmei.com.arindoorlocationsystem.R;
import com.chuangmei.com.arindoorlocationsystem.ar.adapter.VideoListAdapter;
import com.chuangmei.com.arindoorlocationsystem.ar.bean.VideoInfo;
import com.chuangmei.com.arindoorlocationsystem.base.adapter.BaseRecyclerViewAdapter;
import com.chuangmei.com.arindoorlocationsystem.utils.Constant;
import com.chuangmei.com.arindoorlocationsystem.utils.DataUtils;
import com.chuangmei.com.arindoorlocationsystem.utils.FileUtils;
import com.chuangmei.com.arindoorlocationsystem.video.VideoPlayActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：ARIndoorLocationSystem
 * 类描述：自定义视频文件展示Dialog
 * 创建人：LuoHao
 * 创建时间：2018/4/27 13:13
 * 修改人：LuoHao
 * 修改时间：2018/4/27 13:13
 * 修改备注：
 */

public class VideoItemDialog extends Dialog {

    private Context context;
    private RecyclerView mRV_video;

    public VideoItemDialog(@NonNull Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setCanceledOnTouchOutside(false);
        this.context = context;
    }

    public VideoItemDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.context = context;
    }

    protected VideoItemDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_video_item_layout);
        initView();
        initVideoList();
    }

    private void initView() {
        Button mBT_dismiss = (Button) this.findViewById(R.id.mBT_dismiss);
        mBT_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initVideoList() {
        mRV_video = (RecyclerView) findViewById(R.id.mRV_video);
        mRV_video.setLayoutManager(new LinearLayoutManager(context));
        final ArrayList<VideoInfo> video_infos = new ArrayList<>();
        List<File> suffixFiles = FileUtils.getSuffixFile(new ArrayList<File>(), Constant.dir_video, ".mp4");
        for (File file : suffixFiles) {
            long length = file.length();
            if (length > 0) {
                String path = file.getPath();
                String name = file.getName();
                String filesizeFormat = FileUtils.getFilesizeFormat(length);
                String time = DataUtils.DateTimeLong2String(file.lastModified());
                VideoInfo videoInfo = new VideoInfo(path, name, filesizeFormat, time);
                video_infos.add(videoInfo);
            }
        }
        VideoListAdapter videoListAdapter = new VideoListAdapter(R.layout.item_video_info_layout, video_infos);
        mRV_video.setAdapter(videoListAdapter);
        //添加Android自带的分割线
        mRV_video.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));

        videoListAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                VideoInfo videoInfo = video_infos.get(position);
                if (videoInfo != null && FileUtils.fileExists(videoInfo.getVideo_path())) {
                    Intent intent = new Intent(context, VideoPlayActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("video_uri", videoInfo.getVideo_path());
                    bundle.putInt("video_screen_orientation", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }

            @Override
            public void onLongClick(int position) {

            }
        });
    }

}
